package com.lee.boot.head.first.demo.pdf.contoller;


import com.lee.boot.head.first.demo.entity.Student;
import com.lee.boot.head.first.demo.mongo.dao.StudentDao;
import com.lee.boot.head.first.demo.pdf.exporter.PDFExportService;
import com.lee.boot.head.first.demo.pdf.exporter.PDFView;
import com.lowagie.text.*;


import com.lowagie.text.Font;

import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;


import java.awt.*;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(value = "/pdf")
public class PDFController {
    @Autowired
    StudentDao studentDao;

    @RequestMapping(value = "/export")
    public ModelAndView exportPDF(ModelAndView modelAndView) {
        List<Student> students = studentDao.getStudents();
        modelAndView.addObject("students", students);
        View view = new PDFView(getPDFExportService());
        modelAndView.setView(view);

        return modelAndView;
    }

    public PDFExportService getPDFExportService() {
        return (model, document, writer, request, response) -> {
            document.setPageSize(PageSize.A4);
            document.addTitle("学生啊！");
            try {
                document.add(new Chunk("\n"));
                PdfPTable table = new PdfPTable(3);
                //单元格
                PdfPCell cell = null; //字体，定义为蓝色加粗

                Font fB = new Font();
                fB.setColor(Color.blue);
                fB.setStyle(Font.BOLD);
                //标题
                cell = new PdfPCell(new Paragraph("id", fB));
                cell.setHorizontalAlignment(1);
                table.addCell(cell);

                cell = new PdfPCell(new Paragraph("name", fB));
                //居中对齐
                cell.setHorizontalAlignment(1);

                //将单元格加入表格
                table.addCell(cell);
                cell = new PdfPCell(new Paragraph("age", fB));
                //居中对齐
                cell.setHorizontalAlignment(1);
                table.addCell(cell);
                //获取数据模型中的用户列表
                //java.util.List<Student> students = studentDao.getStudents();
                List<Student> students = (List<Student>) model.get("students");
//                BaseFont baseFont = BaseFont.createFont("STSong-Light", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                // 解决itext转pdf不支持中文
                BaseFont baseFont = BaseFont.createFont("src/main/resources/font/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                Font font = new Font(baseFont);
                for (Student student : students) {
                    document.add(new Chunk("\n"));
                    cell = new PdfPCell(new Paragraph(student.getClass().toString()));
                    table.addCell(cell);
                    // 添加中文字体
                    cell = new PdfPCell(new Paragraph(student.getName(), font));
                    table.addCell(cell);
                    cell = new PdfPCell(new Paragraph(student.getAge().toString()));
                    table.addCell(cell);
                }
                document.add(table);
            } catch (DocumentException e) {

            } catch (IOException e) {
                e.printStackTrace();
            }


        };
    }


}
